package controller.content;

import controller.entity.Page;
import controller.entity.Result;
import controller.entity.Tables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.GoodsStoreService;
import service.entity.GoodsStoreDTO;

/**
 * Created by asus on 2017/6/22.
 */
@Controller
@RequestMapping("goodsinstore")
public class GoodsInStroeController {
    @Autowired
    private GoodsStoreService goodsStoreService;

    @RequestMapping("/list")
    @ResponseBody
    public Tables index(@RequestParam(value="page",required = false) String page,
                        @RequestParam(value="rows", required=false) String rows,
                        @RequestParam(value="title", required=false) String title){
        Integer pageNum=1;
        Integer rowsNum=20;//默认每行记录数
        try{
            pageNum=Integer.parseInt(page);
        }catch (Exception e){}
        try {
            rowsNum=Integer.parseInt(rows);
        }catch (Exception e){}
        Page pageBean=new Page(pageNum,rowsNum,1);
        Tables tables=new Tables();
        tables.setTotal(goodsStoreService.getCountGoodsInStore());
        tables.setRows(goodsStoreService.getPageGoodsInStore(pageBean));
        tables.setTitles(title);
        tables.setMethodName("getAllGoodsInStroe");
        System.out.println("pageBean:"+pageBean);
        System.out.println("tables:"+tables);
        return tables;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result addIn(@RequestBody GoodsStoreDTO goodsStoreDTO){
        //首先得验证合法性
        System.out.println("前台传来的goodsStoreDTO原始值："+goodsStoreDTO);
        if(validate(goodsStoreDTO)==false){
            return Result.fail("请输入完整信息");
        }else{
            if(goodsStoreService.insertGoodsInStore(goodsStoreDTO)>0L){
                return Result.success();
            }
            return Result.fail("增加失败");
        }

    }

    /**
     * 用来验证前台传来的GoodsInStoreDTO是否信息完整
     * @param goodsStoreDTO
     * @return
     */
    private boolean validate(GoodsStoreDTO goodsStoreDTO){
        if(goodsStoreDTO.getGoodsId()==null||goodsStoreDTO.getGoodsId()==0){
            return false;
        }
        if(goodsStoreDTO.getStoreId()==null||goodsStoreDTO.getStoreId()==0){
            return false;
        }
        if(goodsStoreDTO.getQuantity()==null||goodsStoreDTO.getQuantity()==0){
            return false;
        }
        if(goodsStoreDTO.getDate()==null){
            return false;
        }
        return true;
    }
}
